using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIRankPanel : MonoBehaviour
{
	public GridLayoutGroup container;

	public UIRankItem rankItem;

	public UIRankItem playerRankItem;

	public UIRankItem playerTopRankItem;

	public Button[] tabs;

	public Sprite[] rankBGItems;

	private bool _showStar = true;

	private List<UIRankItem> _cacheRankItems = new List<UIRankItem>();

	private void Start()
	{
		_cacheRankItems.Clear();
		RankMgr.SortRankInfos(0);
		SetRankItem(playerRankItem, RankMgr.playerRankInfo, RankMgr.playerRank);
		SetRankItem(playerTopRankItem, RankMgr.playerRankInfo, RankMgr.playerRank);
		List<RankInfo> rankInfos = RankMgr.GetRankInfos(0);
		for (int i = 0; i < rankInfos.Count; i++)
		{
			RankInfo rankInfo = rankInfos[i];
			UIRankItem item = SupportTools.AddChild<UIRankItem>(container.gameObject, rankItem.gameObject);
			SetRankItem(item, rankInfo, i);
			_cacheRankItems.Add(item);
		}
		playerRankItem.gameObject.SetActive(value: false);
		playerTopRankItem.gameObject.SetActive(value: false);
		tabs[0].interactable = false;
		tabs[1].interactable = true;
	}

	private UIRankItem GetSelfItem()
	{
		for (int i = 0; i < _cacheRankItems.Count; i++)
		{
			if (_cacheRankItems[i].txtPlayerName.text.Trim().Equals("你"))
			{
				return _cacheRankItems[i];
			}
		}
		return null;
	}

	private void SetRankItem(UIRankItem item, RankInfo rankInfo, int index)
	{
		item.txtPlayerName.text = rankInfo.name;
		item.txtTime.gameObject.SetActive(value: false);
		item.txtScore.text = rankInfo.score + string.Empty;
		item.txtStar.text = rankInfo.star + string.Empty;
		item.txtStar.transform.parent.gameObject.SetActive(_showStar);
		item.txtStar.gameObject.SetActive(_showStar);
		item.txtScore.gameObject.SetActive(!_showStar);
		item.txtRank.text = (index + 1).ToString();
		item.txtDesc.gameObject.SetActive(value: false);
	}

	public void OnSwitch(int type)
	{
		_showStar = (type == 0);
		RankMgr.SortRankInfos(type);
		SetRankItem(playerRankItem, RankMgr.playerRankInfo, RankMgr.playerRank);
		for (int i = 0; i < _cacheRankItems.Count; i++)
		{
			UIRankItem uIRankItem = _cacheRankItems[i];
			SetRankItem(uIRankItem, RankMgr.GetRankInfos(type)[i], i);
			uIRankItem.imgFrame.sprite = rankBGItems[0];
		}
		tabs[type].interactable = false;
		tabs[(type + 1) % 2].interactable = true;
	}

	private void OnEnable()
	{
		UnityEngine.Debug.Log("ONENABLE");
		OnSwitch((!_showStar) ? 1 : 0);
	}

	public void OnScrollChanged(Vector2 pos)
	{
		RectTransform component = container.GetComponent<RectTransform>();
		Vector2 anchoredPosition = component.anchoredPosition;
		float y = anchoredPosition.y;
		Vector2 cellSize = container.cellSize;
		float y2 = cellSize.y;
		int num = Mathf.FloorToInt(y / y2);
		int num2 = num + 5;
		UIRankItem selfItem = GetSelfItem();
		selfItem.imgFrame.sprite = rankBGItems[0];
		if (RankMgr.playerRank >= num && RankMgr.playerRank < num2)
		{
			playerRankItem.gameObject.SetActive(value: false);
			playerTopRankItem.gameObject.SetActive(value: false);
			if (selfItem != null)
			{
				selfItem.imgFrame.sprite = rankBGItems[1];
			}
		}
		else if (RankMgr.playerRank >= num2)
		{
			playerTopRankItem.gameObject.SetActive(value: false);
			playerRankItem.gameObject.SetActive(value: true);
		}
		else if (RankMgr.playerRank < num)
		{
			playerTopRankItem.gameObject.SetActive(value: true);
			playerRankItem.gameObject.SetActive(value: false);
		}
	}

	public void ClickBack()
	{
		AdManagerOld.instance.curAd.HideBanner();
	}
}
